package com.aiit.service.impl;

import com.aiit.constant.MessageConstant;
import com.aiit.constant.StatusConstant;
import com.aiit.context.BaseContext;
import com.aiit.dto.TeaOrderDTO;
import com.aiit.dto.UserAdressDTO;
import com.aiit.dto.UserLoginDTO;
import com.aiit.entity.TeaOrder;
import com.aiit.entity.User;
import com.aiit.entity.UserAddress;
import com.aiit.exception.AccountNotFoundException;
import com.aiit.exception.PasswordErrorException;
import com.aiit.exception.VerificationErrorException;
import com.aiit.mapper.UserMapper;
import com.aiit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDateTime;
import java.util.List;

@Service
@WebServlet("/verifyCode")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void save(UserLoginDTO userLoginDTO) {

        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        User user = new User();

        //对象属性拷贝
        BeanUtils.copyProperties(userLoginDTO, user);

        //设置账户的状态，默认正常状态1表示正常0表示锁定
        user.setStatus(StatusConstant.ENABLE);


        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));

        //设置当前记录的创建时间和修改时间
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人的id
        // TODO 后期需要改为当前登录用户的id
        user.setCreateUser(BaseContext.getCurrentId());
        user.setUpdateUser(BaseContext.getCurrentId());

        userMapper.insert(user);
    }

    @Override
    public User login(UserLoginDTO userLoginDTO, String storedVerificationCode) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        String verification = userLoginDTO.getVerification();

        User user = userMapper.getByUsername(username);

        if (user == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

//        if (employee.getStatus() == StatusConstant.DISABLE) {
//            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
//        }

        if (storedVerificationCode == null || !storedVerificationCode.equalsIgnoreCase(verification)) {
            throw new VerificationErrorException(MessageConstant.VERIFICATION_ERROR);
        }
        return user;
    }

    @Override
    public User pageQuery(UserLoginDTO userLoginDTO) {
        //  PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

//        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
//
//        long total = page.getTotal();
//        List<Employee> records = page.getResult();

        String username = userLoginDTO.getUsername();

        User user = userMapper.getByUsername(username);

        return user;
    }

    @Override
    public UserAddress address(UserAdressDTO userAdressDTO) {

        String username = userAdressDTO.getUsername();

        UserAddress userAddress = userMapper.getByUserAddressname(username);

        return userAddress;
    }


    public void update(UserAdressDTO userAdressDTO) {
        UserAddress userAddress = new UserAddress();
        BeanUtils.copyProperties(userAdressDTO, userAddress);

        userAddress.setCreateAddress(BaseContext.getCurrentId());
        userAddress.setUpdateAddress(BaseContext.getCurrentId());

        try {
            userMapper.update(userAddress);
        } catch (Exception e) {
            // if (e.getMessage() != null && e.getMessage().contains("where username = ?")) {
            userMapper.insertAddress(userAddress);


        }
    }

    @Override
    public List<TeaOrder> order(TeaOrderDTO teaOrderDTO) {
        String username = teaOrderDTO.getUsername();

        List<TeaOrder> teaOrder = userMapper.getByTeaOrder(username);

        return teaOrder;
    }

    @Override
    public void updatePayment(TeaOrderDTO teaOrderDTO) {
        TeaOrder teaOrder = new TeaOrder();

        Long id = teaOrderDTO.getId();
        teaOrder.setPaymentTime(LocalDateTime.now());
        teaOrder.setId(id);
        userMapper.getById(id);
        userMapper.payment(teaOrder);

    }

    @Override
    public TeaOrder orderDetail(TeaOrderDTO teaOrderDTO) {
        Long id = teaOrderDTO.getId();

        TeaOrder teaOrderDteail = userMapper.getByTeaOrderDetail(id);

        return teaOrderDteail;
    }
}
